package com.intelligent.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.intelligent.pojo.Unit;
import com.intelligent.service.UnitService;
import com.intelligent.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 单位控制器
 */
@Api(tags = "单位管理")
@RestController
@RequestMapping("/unit")
public class UnitController {

    @Autowired
    private UnitService unitService;

    @ApiOperation("获取单位列表")
    @GetMapping("/list")
    public Result<List<Unit>> list(@RequestParam(value = "pageSize", defaultValue = "100") Integer pageSize) {
        try {
            LambdaQueryWrapper<Unit> wrapper = new LambdaQueryWrapper<>();
            wrapper.last("LIMIT " + pageSize);
            List<Unit> units = unitService.list(wrapper);
            return Result.success(units);
        } catch (Exception e) {
            return Result.error("获取单位列表失败: " + e.getMessage());
        }
    }

    @ApiOperation("根据ID获取单位")
    @GetMapping("/{id}")
    public Result<Unit> getById(@PathVariable Long id) {
        try {
            Unit unit = unitService.getById(id);
            if (unit == null) {
                return Result.error("单位不存在");
            }
            return Result.success(unit);
        } catch (Exception e) {
            return Result.error("获取单位失败: " + e.getMessage());
        }
    }
}
